package com.axend.aerosense.user.viewmodel;

import android.app.Activity;
import android.app.Dialog;
import android.text.TextUtils;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ToastUtils;
import com.axend.aerosense.base.bean.BaseStringBean;
import com.axend.aerosense.base.impl.IOperationResult;
import com.axend.aerosense.base.storage.MmkvHelper;
import com.axend.aerosense.common.UrlApi;
import com.axend.aerosense.common.bean.UserInfo;
import com.axend.aerosense.common.config.Gobal;
import com.axend.aerosense.common.router.RouterActivityPath;
import com.axend.aerosense.common.ui.LoadingDialog;
import com.axend.aerosense.common.util.CheckUtil;
import com.axend.aerosense.common.util.JsonUtil;
import com.axend.aerosense.network.EasyHttp;
import com.axend.aerosense.network.cache.model.CacheMode;
import com.axend.aerosense.network.callback.ProgressDialogCallBack;
import com.axend.aerosense.network.exception.ApiException;
import com.axend.aerosense.network.subsciber.IProgressDialog;
import com.axend.aerosense.user.R;
import com.axend.aerosense.user.entity.UpdataEmailBean;

import io.reactivex.disposables.Disposable;

public class EmailSettingViewModel extends ViewModel {
    private UserInfo userInfo;
    private Disposable disposable;

    private MutableLiveData<String> email = new MutableLiveData<String>();
    private MutableLiveData<Boolean> canEdit = new MutableLiveData<Boolean>(true);

    public MutableLiveData<Boolean> getCanEdit() {
        return canEdit;
    }

    public MutableLiveData<String> getEmail() {
        return email;
    }

    public void setEmail(Activity activity, IOperationResult operationResult){
        String token = MmkvHelper.getInstance().getMmkv().decodeString(Gobal.SAVE_KEY.TOKEN);
        userInfo = MmkvHelper.getInstance().getMmkv().decodeParcelable(Gobal.SAVE_KEY.USR_INFO, UserInfo.class);
        if(TextUtils.isEmpty(token)){
            return;
        }
        if(!TextUtils.isEmpty(email.getValue()) && !CheckUtil.isEmail(email.getValue())){
            ToastUtils.showShort(R.string.common_input_email);
            return;
        }
//        if(TextUtils.isEmpty(email.getValue())){
//            return;
//        }
        UpdataEmailBean bean = new UpdataEmailBean(email.getValue());
        disposable = EasyHttp.post(UrlApi.URL.SetEmail.getUrl())
                .headers("token", token)
                .cacheMode(CacheMode.NO_CACHE)
                .upJson(JsonUtil.toJson(bean))
                .execute(new ProgressDialogCallBack<BaseStringBean>(LoadingDialog.getProgressDialog(activity)) {
                    @Override
                    public void onTokenExpire() {
                        dismissProgress();
                        ToastUtils.showShort(R.string.common_login_first);
                        ARouter.getInstance().build(RouterActivityPath.Login.PAGE_LOGIN_HOME).withBoolean("jumpMainPage", true).greenChannel().navigation();
                    }

                    @Override
                    public void onError(ApiException e) {
                        dismissProgress();
                        ToastUtils.showShort(e.getMessage());
                        if(null != operationResult){
                            operationResult.onFail();
                        }
                    }

                    @Override
                    public void onSuccess(BaseStringBean bean) {
                        dismissProgress();
                        userInfo.userEmail = email.getValue();
                        MmkvHelper.getInstance().getMmkv().encode(Gobal.SAVE_KEY.USR_INFO, userInfo);
                        ToastUtils.showShort(R.string.common_success);
//                        LiveDatabus.getInstance().with(Gobal.EVENT_KEY.USR_EMAIL_CHANGE, Integer.class).postValue(1);
                        if(null != operationResult){
                            operationResult.onSuccess();
                        }
                    }
                });
    }

    @Override
    protected void onCleared() {
        if(null != disposable && !disposable.isDisposed()){
            disposable.dispose();
            disposable = null;
        }
        super.onCleared();
    }
}
